package com.itheima.edu.manager.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AgeUtil {
    private AgeUtil(){}

    //根据出生日期计算年龄并返回
    public static String getAge(String birthdayStr){
        try{
            //1.将生日字符串解析成对应的日期对象
            Date birthdayDate = new SimpleDateFormat("yyyy-MM-dd").parse(birthdayStr);
            //2.获取当前系统日期
            Date nowDate = new Date();
            //3.计算两者相差多少年
            long age = (nowDate.getTime() - birthdayDate.getTime()) / 1000L / 60 / 60 / 24 /365;
            //4.返回计算的年龄
            return age + "";
        }
        catch (Exception e){
            return "0";
        }
    }
}
